﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace ClassLibrary
{
    public class ErrorManager
    {
        public delegate void ErrorOccurred(string message);
        public static event ErrorOccurred errorOccured;

        public static List<string> log
        {
            get;
            set;
        }

        public static void InitLog()
        {
            string id = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
            Parameters.Values.localPath_File_Log = Parameters.Values.localPath_Dir_Tmp + "\\" + Application.ProductName + "_" + id + ".trace";

            log = new List<string>();
            log.Add("-----------------------------------------------------");
            log.Add("ENTER " + Application.ProductName + " " + DateTime.Now);
            log.Add("-----------------------------------------------------");

        }

        public static void CloseLog()
        {
            StreamWriter logFile = new StreamWriter(Parameters.Values.localPath_File_Log);
            log.Add("-----------------------------------------------------");
            log.Add("EXIT " + Application.ProductName);
            log.Add("-----------------------------------------------------");

            foreach (string s in log)
                logFile.WriteLine(s);
            logFile.Close();
        }

        public static void ShowLog()
        {
            StreamWriter logFile = new StreamWriter(Parameters.Values.localPath_File_Log);
            foreach (string s in log)
                logFile.WriteLine(s);
            logFile.Close();

            Process.Start(Parameters.Values.localPath_File_Log);
        }

        public static void WriteLog(string className, string method, Exception e)
        {
            if (null == log) log = new List<string>();

            log.Add("EXCEPTION IN : " + className + "::" + method);
            log.Add("SOURCE : ");
            log.Add(e.Source);
            log.Add("MESSAGE : ");
            log.Add(e.Message);
            log.Add("STACK : ");
            log.Add(e.StackTrace);

            /*Trace.WriteLine("EXCEPTION IN : " + className + "::" + method);
            Trace.WriteLine("SOURCE : ");
            Trace.WriteLine(e.Source);
            Trace.WriteLine("MESSAGE : ");
            Trace.WriteLine(e.Message);
            Trace.WriteLine("STACK : ");
            Trace.WriteLine(e.StackTrace);*/
        }

        public static void WriteLog(string className, string method, Exception e, string message)
        {
            WriteLog(className, method, e);


            if (null != errorOccured)
                errorOccured(message);
        }

        public static void Clean(int nbFileRemaining)
        {
            string[] files = Directory.GetFiles(System.IO.Path.GetTempPath() + Application.ProductName, Application.ProductName + "*.trace", SearchOption.TopDirectoryOnly);
            System.Collections.ArrayList list = new System.Collections.ArrayList(files);
            list.Sort();
            if (list.Count > nbFileRemaining)
            {
                try
                {
                    for (int i = 0; i < list.Count - nbFileRemaining; i++)
                        File.Delete((string)list[i]);
                }
                catch (Exception e)
                {
                    WriteLog("TraceServices", "Clean", e);
                }
            }
        }

        public static void Clean()
        {
            Clean(10);
        }
    }
}
